package com.init.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.init.common.BaseResponse;
import com.init.common.ErrorCode;
import com.init.common.ResultUtils;
import com.init.exception.BusinessException;
import com.init.model.entity.Categories;
import com.init.service.CategoriesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 大标题接口
 * 获取评定，获取指定评定，修改评定表
 * @author Atanycosts
 * @date 2025/1/5
 */
@RestController
@Slf4j
@RequestMapping("/categories")
public class CategoriesController {

    @Resource
    private CategoriesService categoriesService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public BaseResponse<Page> list(int page, int pageSize, String title) {
        // 已排查
        Page pageBaseResponse = categoriesService.queryPage(page, pageSize, title);

        if (pageBaseResponse.getSize() == 0) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "数据异常,可能为空");
        }
        return ResultUtils.success(pageBaseResponse);
    }


    /**
     * 根据ID查询
     */
    @GetMapping("/{categoriesId}")
    public BaseResponse<Categories> info(@PathVariable("categoriesId") String categoriesId) {
        // 已排查
        Categories byId = categoriesService.queryDetail(categoriesId);

        if (byId == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "请求数据为空");
        }
        return ResultUtils.success(byId);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @PreAuthorize("hasRole('ADMIN') or hasRole('BOSS')") //用户权限
    @Transactional
    public BaseResponse<String> save(@RequestBody Categories categories) {
        // 已排查

        boolean save = categoriesService.save(categories);

        if (save) {
            return ResultUtils.success("新增成功");
        }
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "新增失败");
    }

    /**
     * 修改
     */
    @PatchMapping("/update")
    @PreAuthorize("hasRole('ADMIN') or hasRole('BOSS')") //用户权限
    @Transactional
    public BaseResponse<String> update(@RequestBody Categories categories) {
        // 已排查
        boolean b = categoriesService.updateById(categories);

        if (b) {
            return ResultUtils.success("修改成功");
        }
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "修改失败");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @PreAuthorize("hasRole('ADMIN') or hasRole('BOSS')") //用户权限
    @Transactional
    public BaseResponse<String> delete(@RequestBody Integer[] categoriesIds) {
        // 已排查
        boolean b = categoriesService.removeBatchByIds(Arrays.asList(categoriesIds));

        if (b) {
            return ResultUtils.success("删除成功");
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR, "删除失败,参数为JSON数组");
    }
}
